package leetcode.code0874;

import java.awt.Point;
import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int robotSim(int[] commands, int[][] obstacles) {
		int[] pos = new int[2];
		int[] dir = new int[2];
		Set<Point> obs = new HashSet<>();
		for (int i = 0; i < obstacles.length; i++) {
			obs.add(new Point(obstacles[i][0], obstacles[i][1]));
		}
		dir[1] = 1;
		int ans = 0;
		for (int i = 0; i < commands.length; i++) {
			this.command(pos, dir, commands[i], obs);
			ans = Math.max(ans, pos[0] * pos[0] + pos[1] * pos[1]);
		}
		return ans;
	}

	private void command(int[] pos, int[] dir, int command, Set<Point> obs) {
		if (command == -2) {// 左转
			if (dir[0] == 0) {
				dir[0] = -dir[1];
				dir[1] = 0;
			} else {
				dir[1] = dir[0];
				dir[0] = 0;
			}
		} else if (command == -1) {// 右转
			if (dir[1] == 0) {
				dir[1] = -dir[0];
				dir[0] = 0;
			} else {
				dir[0] = dir[1];
				dir[1] = 0;
			}
		} else {
			for (int i = 1; i <= command && !obs.contains(new Point(pos[0] + dir[0], pos[1] + dir[1])); i++) {
				pos[0] += dir[0];
				pos[1] += dir[1];
			}
		}
	}

	@Override
	public void debug4() {
		H.compare(65, this.robotSim(H.str2array3("[4,-1,4,-2,4]"), H.str2array1("[[2,4]]")));
		H.compare(36, this.robotSim(H.str2array3("[6,-1,-1,6]"), H.str2array1("[]")));
		H.compare(25, this.robotSim(H.str2array3("[4,-1,3]"), H.str2array1("[]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
